import axios from 'axios'
// import qs from "qs";//qs为序列化数据的js库

//设置baseUrl
// axios.defaults.baseURL = process.env.NODE_ENV === 'production' ? 'https://xxxxx' : '/api'
axios.defaults.baseURL = 'http://localhost:8022'

//设置请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
//设置超时
axios.defaults.timeout = 10000
//请求拦截器
axios.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    token && (config.headers.Authorization = token)
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

//响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return Promise.resolve(response)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    console.log(`异常请求：${JSON.stringify(error)}`)
    return Promise.reject(error)
  }
)

//封装post/get请求
export const post = (url, data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url,
      data
      //data: qs.stringify(data), //参数序列化
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export const get = (url, data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      params: data
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err)
      })
  })
}

